package ta.client;

import gwt.trading.client.Attachable;
import gwt.trading.client.DrawingArea;
import gwt.trading.client.DrawingAreaSettings;
import gwt.trading.client.DrawingBoard;
import gwt.trading.client.LinePriceChart;
import gwt.trading.client.MovingAverage;
import gwt.trading.client.StockQuote;
import gwt.trading.client.StockQuoteList;
import gwt.trading.client.TradingChart;
import gwt.trading.client.TradingChartSettings;
import gwt.trading.client.VerticalConstrainter;
import gwt.trading.client.Volume;
import gwt.trading.client.util.ColorEnum;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 * 
 * @author Iulian Costan (iulian.costan@gmail.com)
 * 
 */
public class TAController implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		// define trading chart
		final TradingChartSettings chartSettings = new TradingChartSettings(600, 400);
		final TradingChart tradingChart = new TradingChart(chartSettings);

		// getQuotes(symbol, new AsyncCallback() {
		//
		// public void onFailure(Throwable caught) {
		// GWT.log("cannot get quotes for ", caught);
		// }
		//
		// public void onSuccess(Object result) {
		// StockQuote[] quotes = (StockQuote[]) result;
		// final StockQuoteList quotesList = new StockQuoteList();
		// quotesList.init(quotes);
		//
		// // define volume area
		// VerticalConstrainter volume = new Volume(quotesList, ColorEnum.BLUE);
		// Area volumeArea = new Area(volume, width, 100, new Point(0, 100));
		// tradingChart.add(volumeArea);
		// }
		//			
		// });

		// define chart area
		// MasterAttachable lineChart = new LineChart(quotesList,
		// ColorEnum.RED);
		// Area chartArea = new Area(lineChart, width, 100, new Point(0, 0));
		// tradingChart.add(chartArea);

		StockQuote[] quotes = (StockQuote[]) getQuotes();
		final StockQuoteList quotesList = new StockQuoteList();
		quotesList.init(quotes);

		// line chart area
		VerticalConstrainter lineChart = new LinePriceChart(quotesList, ColorEnum.RED);
		DrawingAreaSettings lineChartSettings = new DrawingAreaSettings(300, ColorEnum.BLACK);

		// volume area
		VerticalConstrainter volume = new Volume(quotesList, ColorEnum.GREEN);
		DrawingAreaSettings volumeSettings = new DrawingAreaSettings(100, ColorEnum.BLACK);
		
		// create areas
		final DrawingArea price = tradingChart.addDrawingArea(lineChart, lineChartSettings);
		tradingChart.addDrawingArea(volume, volumeSettings);

		// simple moving average indicator
		final Attachable sma = new MovingAverage(quotesList);

		final Button button = new Button("Draw Trading Chart");
		button.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				tradingChart.draw();
			}
		});
		final CheckBox smaButton = new CheckBox("Moving Average");
		smaButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				CheckBox checkbox = (CheckBox) sender;
				if (checkbox.isChecked()) {
					price.attach(sma);
				} else {
					price.remove(MovingAverage.ID);
				}
				tradingChart.draw();
			}
		});

		RootPanel.get("draw").add(button);
		RootPanel.get("sma").add(smaButton);
		RootPanel.get().add(tradingChart);
	}

	private void getQuotes(String symbol, AsyncCallback callback) {
		TradingServiceAsync tradingServiceAsync = TradingService.Util.getInstance();
		tradingServiceAsync.getQuotes(symbol, callback);
	}

	public StockQuote[] getQuotes() {
		StockQuote[] quotes = new StockQuote[8];
		quotes[0] = new StockQuote(0, null, 3.18f, 3.23f, 3.17f, 3.18f, 2930430);
		quotes[1] = new StockQuote(1, null, 3f, 3.19f, 2.93f, 3.16f, 10117784);
		quotes[2] = new StockQuote(2, null, 3.26f, 3.28f, 3.02f, 3.02f, 9044662);
		quotes[3] = new StockQuote(3, null, 3.41f, 3.42f, 3.27f, 3.29f, 8144620l);
		quotes[4] = new StockQuote(4, null, 3.30f, 3.42f, 3.30f, 3.40f, 3295144l);
		quotes[5] = new StockQuote(5, null, 3.29f, 3.34f, 3.22f, 3.30f, 2772201l);
		quotes[6] = new StockQuote(6, null, 3.25f, 3.33f, 3.24f, 2.95f, 5407025l);
		quotes[7] = new StockQuote(7, null, 3.11f, 3.23f, 3.06f, 3.2f, 5448892l);
		return quotes;
	}

	public static native void alert(String msg) /*-{
	 alert(msg);
	 }-*/;
}
